package codetop.microsoft.T46;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 18362
 * @Create: 2022-08-14 11:45:43 星期日
 */
class Solution {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> results = new ArrayList<>();
        List<Integer> temp = new ArrayList<>();
        boolean[] used = new boolean[nums.length];
        dfs(results, temp, nums, used);
        return results;
    }

    private void dfs(List<List<Integer>> results, List<Integer> temp, int[] nums, boolean[] used) {
        if (temp.size() == nums.length) {
            results.add(new ArrayList<>(temp));
            return;
        }
        for (int i=0; i<nums.length; ++i) {
            if (!used[i]) {
                used[i] = true;
                temp.add(nums[i]);
                dfs(results, temp, nums, used);
                temp.remove(temp.size()-1);
                used[i] = false;
            }
        }
    }
}